<template>
  <div class="upload">
    <el-upload
      v-loading="loading"
      element-loading-text="上传中..."
      element-loading-spinner="el-icon-loading"
      element-loading-background="rgba(0, 0, 0, 0.8)"
      accept="image/*"
      class="avatar-uploader"
      :action="uploadURL"
      :show-file-list="false"
      :data="data"
      :before-upload="beforeUpload"
      :on-success="successHandle"
    >
      <img v-if="img !== ''" :src="img" class="avatar" />
      <i v-else class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <p class="del" v-if="img !== ''" >
        <i class="el-icon-zoom-in" @click="showPic=true"></i>
      <i class="el-icon-delete" @click="delPic"></i>
    </p>
    <div class="el-dialog__wrapper magnify" style="z-index: 2004" v-if="showPic">
      <div
        role="dialog"
        aria-modal="true"
     
        class="el-dialog"
        style="margin-top: 15vh"
      >
        <div class="el-dialog__header">
          <span class="el-dialog__title"></span
          ><button
            type="button"
            aria-label="Close"
            class="el-dialog__headerbtn"
            @click="showPic=false"
          >
            <i class="el-dialog__close el-icon el-icon-close"></i>
          </button>
        </div>
        <div class="el-dialog__body" >
          <img
            width="100%"
            :src="img"
            alt=""
          />
        </div>
        <!---->
      </div>
    </div>
  </div>
</template>
<script>
import Cookie from "js-cookie";
export default {
  props: {
    img: {
      type: String,
      default: "",
    },
    uploadURL: {
      type: String,
      default: "",
    },
  },
  watch: {
    img(value) {
      console.log(value);
      console.log("图片");
      this.file = value;
    },
    clearList() {
      this.file = "";
    },
    file(value) {
      const list = value;
      console.log(value);
      this.img = value;
    },
  },
  data() {
    return {
      data: {
        TOKEN: Cookie.get("token")
       
      },
       showPic:false,
      loading: false,

      file: this.img ? this.img : "",
    };
  },
  mounted() {},
  methods: {
    // 删除图片
    delPic() {
      this.file = "";
      this.$emit("update", "");
    },
    // 图片上传之前
    beforeUpload() {
      this.loading = true;
    },
    successHandle(response, file, fileList) {
      var url = this.baseUrl + "/sys/file/showpic?id=" + response.result[0];
      this.file = url;
      this.loading = false;
      this.$emit("update", url);
    },
  },
};
</script>
<style >
.magnify {
    background-color: rgba(0, 0, 0, 0.2);
}
.upload {
  position: relative;
  width: 100px;
  height: 100px;
}
.upload:hover .del {
  display: block;
}
.del {
  position: absolute;
  right: 0px;
  top: 0;
  width: 100px;
  height: 100px;
  background: rgba(0, 0, 0, 0.5);
  display: none;
  cursor: pointer;
  line-height: 100px;
  text-align: center;
}
.del i {
  color: #fff;
  font-size: 20px;
}
.upload .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.upload .avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.upload .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
.upload .el-upload--text,
.upload .el-form-item__content {
  width: 100px;
  height: 100px;
  display: block;
}
.upload .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
</style>